import { isUrl } from '../utils/utils';
/* 自定义--菜单 */
import { routerRedux } from 'dva/router';
import { setAuthority } from '../utils/authority';
import { reloadAuthorized } from '../utils/Authorized';
import * as LoginService from '../services/menu';

export default {
  namespace: 'menu',
  state: {
    list: [],
    threeMenu: [],
  },
  effects: {
    // 机构管理
    *fetch_organ(_, { call, put }) {
      const { data: { obj } } = yield call(LoginService.get, '/api/web/menu/getEmployeeMenuList'); /* api/testData/antd/queryAntd  */
      yield put({ type: 'menu', payload: obj });
    },

    *fetch_three_menu(_, { call, put }) {
      const { parentId } = window.sessionStorage;

      const { data: { obj } } = yield call(LoginService.get, '/api/web/menu/getEmployeeMenuButtonList.manage', { parentId }); /* api/testData/antd/queryAntd  */

      yield put({ type: 'threeMenu', payload: obj });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        dispatch({
          type: 'fetch_organ',
        });
      });
    },
  },
  reducers: {
    menu(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    threeMenu(state, action) {
      return {
        ...state,
        threeMenu: action.payload,
      };
    },
  },
};


// function formatter(data, parentPath = '/', parentAuthority) {
//   data = data.obj;
//   return data.map((item) => {
//     let { path } = item;
//     if (!isUrl(path)) {
//       path = parentPath + item.path;
//     }
//     const result = {
//       ...item,
//       path,
//       authority: item.authority || parentAuthority,
//     };
//     if (item.children) {
//       result.children = formatter(item.children, `${parentPath}${item.path}/`, item.authority);
//     }
//     return result;
//   });
// }

// export const getMenuData = () => formatter(menuData);
